package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqStartEnum;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;

import java.lang.ref.WeakReference;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/9 13:40
 *     desc   : 欢迎页
 *     version: 1.0
 * </pre>
 */
public class WelcomeActivity extends BaseActivity {

    private final String TAG = WelcomeActivity.class.getSimpleName();

    private static class MyHandler extends Handler {

        private final WeakReference<WelcomeActivity> mActivity;

        MyHandler(WelcomeActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            WelcomeActivity activity = mActivity.get();
            if (activity != null) {
                if (activity.mDBManager.isFirstRun()) {
                    activity.mDBManager.setFirstRunned();
                    activity.startActivity(new Intent(activity, GuideActivity.class));
                } else {
                    activity.startActivity(new Intent(activity, MainActivity.class));
                }
                activity.finish();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        MyHandler mHandler = new MyHandler(this);
        VolleyRequestUtils.getInstance().getStartEnum(this.toString(), true, false, new ReqStartEnum(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mHandler.sendEmptyMessageDelayed(0, 2000);
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mHandler.sendEmptyMessage(0);
            }
        });
    }

}
